package cn.cqs.redisitem.service;

import cn.cqs.redisitem.dao.RespositoryPcUser;
import cn.cqs.redisitem.entity.PcUser;
import cn.cqs.redisitem.utils.JPASaveUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @discription :用户注册 登录 登出
 * @author: cqs
 * @date: 2020-09-15 8:16
 */
@Service
@Slf4j
public class PCUserServiceimpl implements IPcUserService {
    @Autowired
    RespositoryPcUser repositoryPcUser;

    @Override
    public void addUser(PcUser sysUser) {
        if (ObjectUtils.isNotEmpty(sysUser)) {
            PcUser user = repositoryPcUser.save(sysUser);
            if (ObjectUtils.isNotEmpty(user)) {
                log.info("user", user);
            }
        }
    }

    @Override
    public void updateUser(PcUser sysUser) {
        if (ObjectUtils.isNotEmpty(sysUser)) {
            Integer userid = sysUser.getId();
            if (ObjectUtils.isNotEmpty(userid) && !userid.equals(0)) {
                Optional<PcUser> optional = repositoryPcUser.findById(userid);
                if (optional.isPresent()) {
                    PcUser oldSysuser = optional.get();
//                    拷贝对象属性的值copyProperties(Object source, Object target, String... ignoreProperties)
                    BeanUtils.copyProperties(oldSysuser, sysUser, JPASaveUtil.getNotNUllPropertyNames(sysUser));
                    repositoryPcUser.save(sysUser);
                }
            }
        }
    }

    @Override
    public void deleteUser(PcUser sysUser) {
        if (ObjectUtils.isNotEmpty(sysUser) ){
            repositoryPcUser.delete(sysUser);
        }
    }

    @Override
    public void deletUser(String id) {
        if (StringUtils.isNotBlank(id) && !id.equals("0")) {
            repositoryPcUser.deleteById(Integer.valueOf(id));
        }

    }

    @Override
    public PcUser findById(String id) {
        if (StringUtils.isNotBlank(id) && !id.equals("0")) {
            Optional<PcUser> o = repositoryPcUser.findById(Integer.valueOf(id));
            if (o.isPresent()) {
                return o.get();
            }
        }
        return null;
    }

    @Override
    public PcUser findByName(String name) {
        if (StringUtils.isNotBlank(name)) {
            PcUser sysUser = new PcUser();
            sysUser.setUsername(name);
            Example<PcUser> example = Example.of(sysUser);
            Optional<PcUser> userOptional = repositoryPcUser.findOne(example);
            if (userOptional.isPresent()) {
                return userOptional.get();
            }
        }
        return null;
    }

    @Override
    public List<PcUser> findAll() {
        return repositoryPcUser.findAll();
    }

    @Override
    public List<PcUser> findAll(Page<PcUser> page) {
        PageRequest pageRequest = PageRequest.of(page.getSize(), page.getNumber());
        if (ObjectUtils.isNotEmpty(pageRequest)) {
            Page<PcUser> all = repositoryPcUser.findAll(pageRequest);
            if (ObjectUtils.isNotEmpty(all)) {
                long totalElements = all.getTotalElements();
                int totalPages = all.getTotalPages();
                List<PcUser> content = all.getContent();
                if (ObjectUtils.isNotEmpty(content)) {
                    return content;
                }
            }
        }


        return null;
    }
}
